
import { 
  InsertTextOperation, 
  SetSelectionOperation, 
  RemoveTextOperation
} from './operations'
import { SelectionModel,TextModel,Selection} from './models'
import { KeyboardEvent,SelectionChange} from './event'
import { ViewProvider } from './view/view_provider'
import Operation from './operations/operation';
class Editor{  
  /**可编辑的dom元素 */
  private _container : HTMLElement;
  /**选区模型 */
  private _selectionModel : SelectionModel = new SelectionModel();
  /**数据模型 */
  private _textModel : TextModel = new TextModel();
  /**渲染模式 */
  private _viewProvider : ViewProvider = new ViewProvider()
  constructor(container:HTMLElement){
    this._container = container;
  }

  /**回退 -待插件实现 */
  undo = () =>{}

  /**撤销 -待插件实现 */
  redo = () =>{}

  /* 各个流程数据初始化 */
  init () : void {
    /**事件处理 */
    const eventHandler = new KeyboardEvent(this, this._container)
    const selectionHandler = new SelectionChange(this)
    eventHandler.addListeners()
    selectionHandler.addListeners()

    /**模型视图 */
    this._viewProvider.provide('SOURCE',this._textModel, this._selectionModel, this._container)
    
  }
  getTextModel() :TextModel {
    return this._textModel
  }

  getSelectionModel () :SelectionModel{
    return this._selectionModel
  }

  getViewProvider () : ViewProvider{
    return this._viewProvider
  }

  getContainer() :HTMLElement{
    return this._container
  }
  apply (operation : Operation) : void{
    operation.apply(this);
  }

  /**插入 */
  insertTextAtCursor (text:string) : void{
      const selection = this._selectionModel.getSelection()
      let startIndex = selection.anchor
      let cursorIndex;
      /**选区场景 */
      if (!this._selectionModel.isCollapse()) {
          const removeTextOperation = new RemoveTextOperation(text,selection.anchor, selection.focus)
          this.apply(removeTextOperation)
          /** 对比偏移量 */
          if (this._selectionModel.isBackward()) {
              startIndex = selection.focus;
          }
      }
      cursorIndex = startIndex + text.length
      const insertTextOperation = new InsertTextOperation(text, startIndex);
      this.apply(insertTextOperation)
      this.setSelection(cursorIndex, cursorIndex)
  }

  /**删除 */
  removeTextAtCursor(text: string) {
    const selection = this._selectionModel.getSelection()
    let startIndex : number = selection.anchor
    let endIndex : number = selection.focus
    let cursorIndex : number = 0;
    /**选区场景 */
    if (!this._selectionModel.isCollapse()) {
        const removeTextOperation = new RemoveTextOperation(text,startIndex, endIndex)
        this.apply(removeTextOperation)
        /** 对比偏移量 */
        if (this._selectionModel.isBackward()) {
            startIndex = selection.focus;
        }
        cursorIndex = startIndex
    }else if(startIndex !== 0){
        cursorIndex = startIndex - 1
        const removeTextOperation = new RemoveTextOperation(text,cursorIndex, endIndex)
        this.apply(removeTextOperation)
    } 
    this.setSelection(cursorIndex, cursorIndex)
  }


  /** 中文插入 */
  insertCompositionAtCursor(text:string,selection:Selection) : void{
    let startIndex = selection.anchor
    if (!(selection && selection.anchor === selection.focus)) {
      const removeTextOperation = new RemoveTextOperation(text,selection.anchor,selection.focus)
      this.apply(removeTextOperation)
      if (selection && selection.anchor > selection.focus) {
        startIndex = selection.focus
      }
    }
    const insertTextOperation = new InsertTextOperation(text, startIndex)
    this.apply(insertTextOperation)
    this.setSelection(selection.anchor + text.length, selection.focus + text.length)
  }

  /** 设置区间 */
  setSelection(anchor:number, focus:number) : void {
      const selectionOperaction = new SetSelectionOperation({anchor, focus})
      this.apply(selectionOperaction)
  }
  

}

export default Editor;



